package cn.iocoder.yudao.module.crm.controller.admin.wgm;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.wgm.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.wgm.WgmDO;
import cn.iocoder.yudao.module.crm.service.wgm.WgmService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 外购项目")
@RestController
@RequestMapping("/crm/wgm")
@Validated
public class WgmController {

    @Resource
    private WgmService wgmService;

    @PostMapping("/create")
    @Operation(summary = "创建外购项目")
    @PreAuthorize("@ss.hasPermission('crm:wgm:create')")
    public CommonResult<Long> createWgm(@Valid @RequestBody WgmSaveReqVO createReqVO) {
        return success(wgmService.createWgm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新外购项目")
    @PreAuthorize("@ss.hasPermission('crm:wgm:update')")
    public CommonResult<Boolean> updateWgm(@Valid @RequestBody WgmSaveReqVO updateReqVO) {
        wgmService.updateWgm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除外购项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:wgm:delete')")
    public CommonResult<Boolean> deleteWgm(@RequestParam("id") Long id) {
        wgmService.deleteWgm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得外购项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:wgm:query')")
    public CommonResult<WgmRespVO> getWgm(@RequestParam("id") Long id) {
        WgmDO wgm = wgmService.getWgm(id);
        return success(BeanUtils.toBean(wgm, WgmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得外购项目分页")
    @PreAuthorize("@ss.hasPermission('crm:wgm:query')")
    public CommonResult<PageResult<WgmRespVO>> getWgmPage(@Valid WgmPageReqVO pageReqVO) {
        PageResult<WgmDO> pageResult = wgmService.getWgmPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WgmRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出外购项目 Excel")
    @PreAuthorize("@ss.hasPermission('crm:wgm:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWgmExcel(@Valid WgmPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WgmDO> list = wgmService.getWgmPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "外购项目.xls", "数据", WgmRespVO.class,
                        BeanUtils.toBean(list, WgmRespVO.class));
    }

}